package com.fulihui.wgj.admin.weixin.impl;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.enums.CustomMsgTypeEnum;
import com.fulihui.common.enums.MsgTypeEnum;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.TextMsgUtil;
import com.fulihui.weixinsdk.WeixinSDK;
import com.fulihui.weixinsdk.model.Material;
import com.fulihui.weixinsdk.request.customer.CustomerImgMsgRequest;
import com.fulihui.weixinsdk.request.customer.CustomerMPNewsMsgRequest;
import com.fulihui.weixinsdk.request.customer.CustomerTextMsgRequest;
import com.fulihui.weixinsdk.result.customer.CustomerImgMsgResult;
import com.fulihui.weixinsdk.result.customer.CustomerMPNewsMsgResult;
import com.fulihui.weixinsdk.result.customer.CustomerTextMsgResult;
import com.fulihui.wgj.admin.integration.WxPlatformClient;
import com.fulihui.wgj.admin.weixin.WeiXinCustomerMsgManage;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.fulihui.common.error.Errors.WxSystemConfigError.GET_TOKEN_FAILED;

/**
 * @author: Levon
 * @date: 2018-07-30 16:52
 */
@Service
public class WeiXinCustomerMsgManageImpl implements WeiXinCustomerMsgManage {

    @Autowired
    private WxPlatformClient wxPlatformClient;

    @Autowired
    private WeixinSDK weixinSDK;

    @Override
    public Result<Boolean> sendText(String appId, String openid, String msgType, String content, String mediaId) {
        String accessToken = wxPlatformClient.queryTokenFromRedis(appId);
        if (StringUtils.isBlank(accessToken)) {
            return JsonResultBuilder.fail(GET_TOKEN_FAILED.getErrcode(), GET_TOKEN_FAILED.getErrmsg());
        }

        if (StringUtils.equals(CustomMsgTypeEnum.text.getCode(), msgType)) {

            CustomerTextMsgRequest request = new CustomerTextMsgRequest();
            request.setAccess_token(accessToken);
            request.setTouser(openid);
            request.setMsgtype(MsgTypeEnum.text.getCode());
            Map<String, String> contentParm = Maps.newHashMap();
            String escapeContnet = TextMsgUtil.escape(content);
            contentParm.put("content", escapeContnet);
            request.setText(contentParm);
            CustomerTextMsgResult customerTextMsgResult = weixinSDK.invokeService(request);
            if (customerTextMsgResult.isSuccess()) {
                return JsonResultBuilder.succ(true);
            }
        } else if (StringUtils.equals(CustomMsgTypeEnum.image.getCode(), msgType)) {
            CustomerImgMsgRequest request = new CustomerImgMsgRequest();
            request.setAccess_token(accessToken);
            request.setTouser(openid);
            Material material = new Material();
            material.setMedia_id(mediaId);
            request.setImage(material);
            CustomerImgMsgResult customerImgMsgResult = weixinSDK.invokeService(request);
            if (customerImgMsgResult.isSuccess()) {
                return JsonResultBuilder.succ(true);
            }
        } else if (StringUtils.equals(CustomMsgTypeEnum.mpnews.getCode(), msgType)) {
            CustomerMPNewsMsgRequest request = new CustomerMPNewsMsgRequest();
            request.setAccess_token(accessToken);
            request.setTouser(openid);
            Material material = new Material();
            material.setMedia_id(mediaId);
            request.setMpnews(material);
            CustomerMPNewsMsgResult customerMPNewsMsgResult = weixinSDK.invokeService(request);
            if (customerMPNewsMsgResult.isSuccess()) {
                return JsonResultBuilder.succ(true);
            }
        }
        return JsonResultBuilder.fail(500, "发送预览失败，不明确的消息类型");
    }
}
